const util = require("../../../utils/util.js");

const app = getApp()

Page({

    data: {
        payed: false,
        plcaceHolder: '评论'
    },

    onLoad: function (options) {
        console.log(options.cId)
        this.setData({
            fileId: options.cId,
            openid: app.globalData.openid
        })
        // 获取景点详情信息
        this.getDetail()
        // 获取商品信息
        this.getGoods(options.cId)
        // 获取收藏情况
        this.getCollectStatus()
    },
    // 获取商品信息
    getGoods(jdid) {
        var that = this;
        wx.request({
            url: app.globalData.baseUrl + 'api/jdsp/v1/wx/select',
            method: 'POST',
            data: {
                jdid
            },
            success(res) {
                that.setData({
                    list: res.data
                })

            }
        })
    },
    /**
     * 获取景点信息
     */
    getDetail(cId) {
        var that = this;
        wx.request({
            url: app.globalData.baseUrl + 'api/spots/v1/wx/getAll',
            method: 'POST',
            data: {
                cId: this.data.fileId
            },
            success: function (res) {
                var action = res.data[0]
                action.time = util.formatTime(new Date(action.time))
                // 评论点赞等处理
                for (var l in action.prizeList) {
                    if (action.prizeList[l].openid == app.globalData.openid) {
                        action.isPrized = true
                    }
                }
                for (var l in action.commentList) {
                    action.commentList[l].time = util.formatTime(new Date(action.commentList[l].time))
                }

                that.setData({
                    file: action
                })
            }
        })
    },





    getInputValue(event) {

        console.log(event.detail.value)

        this.data.inputValue = event.detail.value

    },

    /**
     * 点赞或者取消操作
     * @param {}} event 
     */
    pirzeAction(event) {
        var that = this;
        if (app.globalData.userInfo == null) {
            wx.switchTab({
                url: '/pages/me/me',
                success(res) {
                    wx.showToast({
                        title: '请登录',
                        icon: 'none'
                    })
                }
            })
        } else {
            console.log(that.data.fileId)
            var that = this;
            wx.request({
                url: app.globalData.baseUrl + 'api/spots/v1/wx/getAll',
                method: 'POST',
                data: {
                    cId: that.data.fileId
                },
                success(res) {
                    console.log(res)
                    var action = res.data[0]
                    if (action.prizeList == null) {
                        action.prizeList = []
                    }
                    var tag = false
                    var index
                    for (var l in action.prizeList) {
                        if (action.prizeList[l].openid == app.globalData.openid) {
                            tag = true
                            index = l
                            break
                        }
                    }
                    if (tag) {
                        //之前点赞过 删除点赞记录
                        //解决手机取消点赞时候设置为null的bug
                        let prizeList = []
                        for (let i in action.prizeList) {
                            if (index != i) {
                                prizeList.push(action.prizeList[i])
                            }
                        }
                        action.prizeList = prizeList;
                        console.log(action)
                        wx.request({
                            url: app.globalData.baseUrl + 'api/spots/v1/wx/saveOrUpdate',
                            method: 'POST',
                            data: action,
                            success(res) {
                                that.getDetail()
                            }
                        })
                    } else {
                        //之前未点赞  添加点赞记录
                        var user = {}
                        user.nickName = app.globalData.userInfo.cNickName
                        user.faceImg = app.globalData.userInfo.cIcon
                        user.openid = app.globalData.openid
                        if (!action.prizeList) {
                            action.prizeList = []
                        }
                        action.prizeList.push(user)

                        console.log(action.prizeList)
                        wx.request({
                            url: app.globalData.baseUrl + 'api/spots/v1/wx/saveOrUpdate',
                            method: 'POST',
                            data: action,
                            success(res) {
                                wx.showToast({
                                    title: '点赞成功！',
                                })
                                that.getDetail()
                            }
                        })
                    }

                }
            })

        }

    },

    /**
     * 添加评论
     */
    publishComment() {
        var that = this;

        if (app.globalData.userInfo == null) {
            wx.switchTab({
                url: '/pages/me/me',
                success(res) {
                    wx.showToast({
                        title: '请登录',
                        icon: 'none'
                    })
                }
            })
        } else {
            console.log(that.data.fileId)
            var that = this;
            wx.request({
                url: app.globalData.baseUrl + 'api/spots/v1/wx/getAll',
                method: 'POST',
                data: {
                    cId: that.data.fileId
                },
                success(res) {
                    console.log(res)
                    var action = res.data[0]
                    var comment = {}
                    comment.nickName = app.globalData.userInfo.cNickName
                    comment.faceImg = app.globalData.userInfo.cIcon
                    comment.openid = app.globalData.openid
                    comment.text = that.data.inputValue
                    comment.time = Date.now()
                    comment.toOpenid = that.data.toOpenid
                    comment.toNickname = that.data.toNickname
                    if (action.commentList == null) {
                        action.commentList = []
                    }
                    action.commentList.push(comment)

                    wx.request({
                        url: app.globalData.baseUrl + 'api/spots/v1/wx/saveOrUpdate',
                        method: 'POST',
                        data: action,
                        success(res) {
                            wx.showToast({
                                title: '评论成功！',
                            })
                            that.getDetail()
                            that.setData({
                                inputValue: '',
                                plcaceHolder: '评论',
                                toOpenid: '',
                            })
                        }
                    })
                }
            })

        }


    },
    /**
     * 删除评论
     * @param {*} event 
     */
    deleteComment(event) {
        var that = this;
        console.log(event.currentTarget.dataset.index)
        wx.showModal({
            title: '提示',
            content: '确定要删除此评论吗？',
            success(res) {
                if (res.confirm) {
                    var index = event.currentTarget.dataset.index
                    wx.request({
                        url: app.globalData.baseUrl + 'api/spots/v1/wx/getAll',
                        method: 'POST',
                        data: {
                            cId: that.data.fileId
                        },
                        success(res) {
                            console.log(res)
                            var action = res.data[0]
                            action.commentList.splice(index, 1)
                            wx.request({
                                url: app.globalData.baseUrl + 'api/spots/v1/wx/saveOrUpdate',
                                method: 'POST',
                                data: action,
                                success(res) {
                                    console.log(res)
                                    wx.showToast({
                                        title: '删除成功',
                                    })
                                    that.getDetail()
                                }
                            })
                        }
                    })
                } else if (res.cancel) {

                }
            }
        })

    },
    huifuComment(event) {

        console.log(event.currentTarget.dataset.index)
        var index = event.currentTarget.dataset.index

        this.setData({
            plcaceHolder: '回复' + this.data.file.commentList[index].nickName,
            toOpenid: this.data.file.commentList[index].openid,
            toNickname: this.data.file.commentList[index].nickName,
        })
    },

    toYuyue(event) {
        let index = event.currentTarget.dataset.index
        wx.navigateTo({
            url: '/pages/index/spotDetail/yuyue/yuyue?goodName=' + this.data.list[index].cName + '&price=' + this.data.list[index].dPrice + '&id=' + this.data.fileId,
        })
    },

    opendLocation() {
        var that = this;
        wx.openLocation({
            latitude: Number(that.data.file.cLatitude),
            longitude: Number(that.data.file.cLongitude),
        })
    },

    onShareAppMessage(event) {
        if (event.from == 'button') {
            console.log(event.target.dataset.index)
            var index = event.target.dataset.index
            return {
                title: this.data.file.cName,
                imageUrl: this.data.file.cCover,
                path: 'pages/detail/detail?id=' + this.data.file.cId
            }
        }
        if (event.from == 'menu') {
            return {
                title: this.data.file.cName,
                imageUrl: this.data.file.cCover,
                path: 'pages/detail/detail?id=' + this.data.file.cId
            }
        }


    },

    //收藏
    collect() {
        var that = this;
        wx.request({
            url: app.globalData.baseUrl + 'api/collect/v1/wx/addCollect',
            method: 'POST',
            data: {
                cCover: this.data.file.cCover,
                cName: this.data.file.cName,
                dPrice: this.data.file.dPrice,
                cSpotid: this.data.file.cId,
                cWxOpenId: app.globalData.openid
            },
            success(res) {
                wx.showToast({
                    title: '收藏成功',
                })
                that.getCollectStatus()
            }
        })
    },

    //获取收藏情况
    getCollectStatus() {
        var that = this;
        wx.request({
            url: app.globalData.baseUrl + 'api/collect/v1/wx/getCollect',
            method: 'POST',
            data: {
                cWxOpenId: app.globalData.openid,
                cSpotid: this.data.fileId
            },
            success(res) {
                if (res.data.data.length > 0) {
                    that.setData({
                        isCollected: true
                    })
                } else {
                    that.setData({
                        isCollected: false
                    })
                }
            }
        })
    },

    // 取消搜藏
    cancelCollect() {
        var that = this;
        wx.request({
            url: app.globalData.baseUrl + 'api/collect/v1/wx/removeCollect',
            method: 'POST',
            data: {
                cWxOpenId: app.globalData.openid,
                cSpotid: this.data.fileId
            },
            success(res) {
                wx.showToast({
                    title: '取消成功',
                })
                that.getCollectStatus()
            }
        })
    }
})